#include "musicplayerwindow.h"

#include <QTcpSocket>
#include <QVBoxLayout>
#include <QPushButton>
#include <QSlider>
#include <QFileDialog>
#include <QLabel>

#include "jsonrpcsocket.h"
#include "jsonrpcresponsehandler.h"
#include "jsonrpcnotificationhandler.h"


MusicPlayerWindow::MusicPlayerWindow(QWidget *parent) : QMainWindow(parent)
{
    QWidget *mainWidget = new QWidget();
    QWidget *effectDisplayerWidget = new QWidget();
    QVBoxLayout *vbox = new QVBoxLayout();
    QHBoxLayout *controlLayout = new QHBoxLayout();
    volumeSlider = new QSlider(Qt::Horizontal);
    volumeSlider->setRange(0, 100);
    timeSlider = new QSlider(Qt::Horizontal);
    titleLabel = new QLabel("Title");

    QPushButton *playPauseButton = new QPushButton("Play/Pause");
    QPushButton *stopButton = new QPushButton("Stop");
    QPushButton *loadButton = new QPushButton("Load");



    effectDisplayerWidget->setStyleSheet("background:#000000");
    mainWidget->setLayout(vbox);

    vbox->addWidget(titleLabel, 4);
    vbox->addWidget(effectDisplayerWidget, 60);
    vbox->addWidget(timeSlider, 10);
    vbox->addLayout(controlLayout,15);

    controlLayout->addWidget(playPauseButton);
    controlLayout->addWidget(stopButton);
    controlLayout->addWidget(loadButton);
    controlLayout->addWidget(volumeSlider);
    this->setCentralWidget(mainWidget);


    QTcpSocket *tcpsocket = new QTcpSocket();
    tcpsocket->connectToHost("127.0.0.1", 5000);
    p_socket = new JsonRpcSocket(tcpsocket);


    QObject::connect(loadButton, SIGNAL(clicked()), this, SLOT(loadTrack()));
    QObject::connect(playPauseButton, SIGNAL(clicked()), this, SLOT(playPause()));
    QObject::connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));

    QObject::connect(timeSlider, SIGNAL(sliderPressed()), this, SLOT(pressSlider()));
    QObject::connect(timeSlider, SIGNAL(sliderReleased()), this, SLOT(releaseSlider()));

    JsonRpcNotificationHandler *handle;
    p_socket->handleNotification("setMetaData(QMap<QString,QVariant>)", this, "updateTrackInformation(QMap<QString,QVariant>)");
    p_socket->handleNotification("setPosition(qint64)", this, "updatePosition(qint64)");
    p_socket->handleNotification("setTimeRange(int,int)", this, "updateTimeRange(int,int)");
}

MusicPlayerWindow::~MusicPlayerWindow()
{
}


void MusicPlayerWindow::loadTrack(){
    QString	filename = QFileDialog::getOpenFileName(this, "Select a track", "/home/clems", "Music (*.mp3)");
    if(filename == "")  return;
    JsonRpcResponseHandler *resp;
    resp = p_socket->invokeMethod("MusicController.load(QString)", filename);
}



void MusicPlayerWindow::updatePosition(qint64 position){
    if(position <= timeSlider->maximum() && !v_timeSliderPressed){
        timeSlider->setValue(position);
    }
}


void MusicPlayerWindow::updateTimeRange(int min, int max){
    timeSlider->setRange(min, max);
}

void MusicPlayerWindow::updateVolume(int vol){
    qint64 volume = vol;
    volumeSlider->setValue(volume);
}



void MusicPlayerWindow::updateTrackInformation(QMap<QString,QVariant> metaData){

    QString title = metaData.value("Title").toString();
    titleLabel->setText(title);
}



void MusicPlayerWindow::playPause(){
    JsonRpcResponseHandler *resp;
    resp = p_socket->invokeMethod("MusicController.playPause()");
    resp->deleteLater();
}


void MusicPlayerWindow::stop(){
    p_socket->invokeMethod("MusicController.stop()");
}
